<?php
/**
 * @author yuanyitian
 * @date 2024/10/23 1:52
 */

namespace api\admin;

class specs
{
    //获取全量列表
    public function getAll(){
        $con=con();
        $data=mysqli_query($con,"select a.id,IFNULL(CONCAT(a.name,'(',GROUP_CONCAT(b.`value`),')'),a.name) AS `name`  from specs a LEFT JOIN specs_value_config b on (a.id=b.specs_id and b.is_delete=1 and b.`status`=1) where a.is_delete=1 and a.`status`=1 GROUP BY a.id ");
        $data=mysqli_fetch_all($data,true);
        mysqli_close($con);
        return out_right($data);
    }

    //分页列表
    public function getList($data){
        $page=(int)$data['page'];
        $size=(int)$data['size'];
        $name=$data['name'];

        $where=' where a.is_delete=1 ';
        if(!empty($name)){
            $where.=" and a.name like '%$name%'";
        }
        $start=($page-1)*$size;
        $limit=" limit $start,$size";

        $con=con();
        $sql="SELECT a.*,GROUP_CONCAT(b.value) AS value FROM `specs` a left join specs_value_config b on (a.id=b.specs_id and b.is_delete=1 and b.status=1) $where  GROUP BY a.id  ORDER BY a.create_time DESC ";
        $data=mysqli_query($con,$sql.$limit);
        $data=mysqli_fetch_all($data,true);
        $total=mysqli_query($con,$sql);
        $total=mysqli_num_rows($total);
        mysqli_close($con);
        for($i=0;$i<count($data);$i++){
            if(empty($data[$i]['create_time'])){
                $data[$i]['create_time']='';
            }else{
                $data[$i]['create_time']=date('Y-m-d H:i:s',$data[$i]['create_time']);
            }
            if(empty($data[$i]['update_time'])){
                $data[$i]['update_time']='';
            }else{
                $data[$i]['update_time']=date('Y-m-d H:i:s',$data[$i]['update_time']);
            }
            if(empty($data[$i]['value'])){
                $data[$i]['value']=[];
            }else{
                $data[$i]['value']=explode(',',$data[$i]['value']);
            }
            if($data[$i]['status']==1){
                $data[$i]['status']=true;
            }else{
                $data[$i]['status']=false;
            }
        }

        return out_right([
            'data'=>$data,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size)
        ]);
    }

    //修改状态
    public function changeStatus($data){
        $id=(int)$data['id'];
        $status=(int)$data['status'];
        $time=time();

        $con=con();
        $res=mysqli_query($con,"update specs set status=$status,update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error('修改失败！');
        }
    }

    //删除规格
    public function del($data){
        $id=(int)$data['id'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update specs set is_delete=0,update_time=$time,delete_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error('删除失败！');
        }
    }

    //添加规格
    public function add($data){
        $name=$data['name'];
        $remark=$data['remark'];
        $value=$data['value'];
        if(empty($value)){
            return out_error('添加失败！请设置属性值！');
        }
        $time=time();

        $con=con();
        $id=mysqli_query($con,"insert into specs(name,remark,create_time,update_time) values('$name','$remark',$time,$time)");
        if($id){
            $id=mysqli_insert_id($con);
            $sql=[];
            for($i=0;$i<count($value);$i++){
                $sql[]="($id,'".$value[$i]."',$time,$time)";
            }
            $sql=implode(',',$sql);
            mysqli_query($con,"insert into specs_value_config(specs_id,value,create_time,update_time) values$sql");

            mysqli_close($con);
            return out_right();
        }else{
            mysqli_close($con);
            return out_error('添加失败！');
        }
    }

    //编辑规格
    public function edit($data){
        $id=(int)$data['id'];
        $name=$data['name'];
        $remark=$data['remark'];
        $value=$data['value'];
        if(empty($value)){
            return out_error('修改失败！请设置属性值！');
        }
        $time=time();

        $con=con();
        $res=mysqli_query($con,"update specs set name='$name',remark='$remark',create_time=$time,update_time=$time where id=$id");
        $res=mysqli_query($con,"update specs_value_config set is_delete=0,update_time=$time,delete_time=$time where specs_id=$id");
        if($res){
            $sql=[];
            for($i=0;$i<count($value);$i++){
                $sql[]="($id,'".$value[$i]."',$time,$time)";
            }
            $sql=implode(',',$sql);
            mysqli_query($con,"insert into specs_value_config(specs_id,value,create_time,update_time) values$sql");

            mysqli_close($con);
            return out_right();
        }else{
            mysqli_close($con);
            return out_error('编辑失败！');
        }
    }


}